/**
    [Filename]
    Purpose: [Purpose]

    @author Jason Wojcik
    @version [version]
*/

#include "CMap.h"

CMap::CMap() {
    Surf_Tileset = NULL;
}

/*
Creates a vector of tiles
Map file:
    A list of whitespace separated A:B
    A is the tile sprite (start at 0, move left by tile width)
    B is the type type - per the CTile.h enum, set to zero to not display
@param file name of the map file
@return  true/false
*/
bool CMap::OnLoad(char* File) {
    TileList.clear();

    FILE* FileHandle = fopen(File, "r");

    if(FileHandle == NULL) {
        return false;
    }

    //iterates row by row, left to right over the map file, adding the tile to the tile vector
    for(int Y = 0;Y < MAP_HEIGHT;Y++) {
        for(int X = 0;X < MAP_WIDTH;X++) {
            CTile tempTile;

            //reads the tileID and typeID from the opened map file into a temp tile
            fscanf(FileHandle, "%d:%d ", &tempTile.TileID, &tempTile.TypeID);

            //adds the temp tile into the tile vector
            TileList.push_back(tempTile);
        }
        fscanf(FileHandle, "\n");
    }

    //close the file
    fclose(FileHandle);

    return true;
}

/*
renders the map one tile at a time
@params screen, x and y coordinates for the upper left corner of the map on screen
*/
void CMap::OnRender(SDL_Surface* Surf_Display, int MapX, int MapY) {
    if(Surf_Tileset == NULL) return;

    //
    int TilesetWidth  = Surf_Tileset->w / TILE_SIZE;
    int TilesetHeight = Surf_Tileset->h / TILE_SIZE;

    int ID = 0;

    for(int Y = 0;Y < MAP_HEIGHT;Y++) {
        for(int X = 0;X < MAP_WIDTH;X++) {
            //do not draw the tile if the ID is NONE
            if(TileList[ID].TypeID == TILE_TYPE_NONE) {
                ID++;
                continue;
            }

            //x and y coordinates for the tile on screen
            int tX = MapX + (X * TILE_SIZE);
            int tY = MapY + (Y * TILE_SIZE);

            //X2 and Y2 coordinates for the sub-image within the sprite sheet
            int TilesetX = (TileList[ID].TileID % TilesetWidth) * TILE_SIZE;
            int TilesetY = (TileList[ID].TileID / TilesetWidth) * TILE_SIZE;

            //draw the tile (defined by the sub-image) at tX, tY
            CSurface::OnDraw(Surf_Display, Surf_Tileset, tX, tY, TilesetX, TilesetY, TILE_SIZE, TILE_SIZE);

            ID++;
        }
    }
}


/*
Gets a copy of the tile at coordinates X,Y
@param screen X and Y coordinate to test
@return  reference to the tile object at the supplied X,Y
*/
CTile* CMap::GetTile(int X, int Y) {
    int ID = 0;

    ID = X / TILE_SIZE;
    ID = ID + (MAP_WIDTH * (Y / TILE_SIZE));

    if(ID < 0 || ID >= TileList.size()) {
        return NULL;
    }

    return &TileList[ID];
}

/*
Gets a copy of the tile
@param ID
@return  Tile of provided ID
*/
CTile* CMap::GetTile(int ID) {
    return &TileList[ID];
}


/*
Sets the tile color
@params ID of the tile, color to set
@return N/A
*/
void CMap::SetTileColor(int X, int Y, int Color){

    int ID = 0;

    ID = X / TILE_SIZE;
    ID = ID + (MAP_WIDTH * (Y / TILE_SIZE));

    if(ID < 0 || ID >= TileList.size()) {
        return;
    }

    TileList[ID].TileColor = Color;
}
